﻿package com.wit.dao.impl;

import com.wit.dao.AreaDao;
import java.util.List;
import com.wit.po.Area;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import hibernate.HibernateSessionFactory;

public class AreaDaoImpl implements AreaDao
{
	public Area getAreaById(String areaId)
	{
		if(areaId == null)
			return null;
		Session session = null;
		Area area = null;
		try {
			
			String hql = "from Area area where area.areaId=?";
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			query.setString(0, areaId);
			area = (Area)query.uniqueResult();
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return area;
	}
	
	static public List<Area> s_GetAreaList()
	{
		Session session = null;
		List<Area> areaList = null;
		try {
			String hql = "from Area area order by area.areaId";
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql);
			areaList = (List<Area>)query.list();
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return areaList;
	}
	
	public List<Area> getAreaList()
	{
		return AreaDaoImpl.s_GetAreaList();
	}
	
	public boolean addArea(Area area)
	{
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			session.save(area);
			tx.commit();
			flag = true;
		
		} catch (Exception e) {
		
			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}
	
	public boolean removeArea(Area area)
	{
		if(isAreaRemovable(area.getAreaId()))
		{
			Session session = null;
			boolean flag = false;
			try {
				session = HibernateSessionFactory.getSession();
				Transaction tx = session.beginTransaction();
				session.delete(area);
				tx.commit();
				flag = true;
			} catch (Exception e) {
				flag = false;
			} finally {

				if(session != null && session.isOpen())
					session.close();
			}
			return flag;
		}
		return false;
	}
	
	public boolean updateArea(Area area)
	{
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Transaction tx = session.beginTransaction();
			session.update(area);
			tx.commit();
			
			flag = true;
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}
	
	public boolean isAreaRemovable(String areaId)
	{
		if(areaId == null)
			return false;
		
		String hql1 = "select count(*) from Area a where a.supAreaId=? and a.areaId <> ?";
		String hql2 = "select count(*) from Client client where client.area.areaId=?";
		Session session = null;
		boolean flag = false;
		try {
			session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(hql1);
			query.setString(0,areaId);
			query.setString(1,areaId);
			
			if(((Long)query.uniqueResult()) > 0)
			{
				flag = false;
			}
			else
			{
				query = session.createQuery(hql2);
				query.setString(0,areaId);
				if(((Long)query.uniqueResult()) > 0)
				{
					flag = false;
				}
				else
				{
					flag = true;
				}
			}
		
		} catch (Exception e) {

			flag = false;
		} finally {

			if(session != null && session.isOpen())
				session.close();
		}
		return flag;
	}
	
}